<template>
	<view>
		<u-sticky><u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs></u-sticky>
		<u-card padding="16" v-if="current == 0 && dataList">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="10" text-align="left">
							<view class="weikaishi2 lanse">
								<span v-html="'库位：'+dataList.positionNumber"></span>
							</view>
						</u-col>
						<u-col span="2" text-align="center">
							<view class="u-line-1">
								<span><u-button type="default" plain="true" size="mini" @click="add()">新增</u-button></span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'数量：'+dataList.num"></span>
							</view>
						</u-col>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'体积：'+dataList.volume"></span>
							</view>
						</u-col>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'托数：'+dataList.trayNum"></span>
							</view>
						</u-col>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'品类：'+dataList.goodsNum"></span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
		</u-card>
		
		<u-card padding="16" v-for="(item,index) in dataList.list" :key="index" v-if="current == 0 && dataList">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="12" text-align="left">
							<view class="weikaishi4">
								<span>名称：{{item.goodsName}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="6" text-align="left">
							<view class="weikaishi6 u-line-1">
								<span>编号：{{item.goodsNumber}}</span>
							</view>
						</u-col>
						<u-col span="6" text-align="center">
							<view class="weikaishi6 u-line-1">
								<span>货号：{{item.goodsNo}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="6" text-align="left">
							<view class="weikaishi6 u-line-1">
								<span>包装：{{item.goodsPacking}}</span>
							</view>
						</u-col>
						<u-col span="6" text-align="center">
							<view class="weikaishi6 u-line-1">
								<span>{{item.num}}*{{item.trayNum}}托</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="7" text-align="left">
							<view class="weikaishi6 u-line-1">
								<span>单位：{{item.goodsUnit}}</span>
							</view>
						</u-col>
						<u-col span="5" text-align="center">
							<view class="weikaishi6 u-line-1">
								<span>规格：{{item.goodsSpecs}}</span>
							</view>
						</u-col>
						<u-col span="12" text-align="left" v-if="item.xianggui">
							<view class="weikaishi6">
								<span>
								换算：{{parseInt(item.num/item.xianggui)}}箱{{item.num-parseInt(item.num/item.xianggui)*item.xianggui}}盒
								</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="wrap">
					<u-collapse>
						<u-collapse-item title="详情">
							<u-row>
								<u-col span="5" text-align="center">
									<view class="weikaishi4 u-line-1">
										<span>批次</span>
									</view>
								</u-col>
								<u-col span="3" text-align="center">
									<view class="weikaishi4 u-line-1">
										<span>托盘</span>
									</view>
								</u-col>
								<u-col span="4" text-align="center">
									<view class="weikaishi4 u-line-1">
										<span>库存数</span>
									</view>
								</u-col>
							</u-row>
							<u-row v-for="(item1,index1) in item.list2" :key="index1">
								<u-col span="5" text-align="center">
									<view class="u-line-1 u-m-t-10">
										<span>{{item1.goodsbatchs}}</span>
									</view>
								</u-col>
								<u-col span="3" text-align="center">
									<view class="u-line-1 u-m-t-10">
										<span>{{item1.trayNumber}}</span>
									</view>
								</u-col>
								<u-col span="4" text-align="center">
									<view class=" u-m-t-10">
										<span>{{item1.num}}</span>
									</view>
								</u-col>
								<u-col span="12" text-align="left" v-if="item1.xianggui">
									<view class="weikaishi6">
										<span>
										换算：{{parseInt(item1.num/item1.xianggui)}}箱{{item1.num-parseInt(item1.num/item1.xianggui)*item1.xianggui}}盒
										</span>
									</view>
								</u-col>
								<u-col span="12" text-align="right">
									<view class="u-m-t-4">
										<span><u-button type="primary" plain="true" size="default" @click="chayi(item1)">差异</u-button></span>
									</view>
								</u-col>
							</u-row>
						</u-collapse-item>
					</u-collapse>
				</view>
			</view>
			<view class="foot-butten" slot="foot">
				<view :class="flex">
					<u-col span="12" text-align="right">
						<view class="">
							<span><u-button type="primary" plain="true" size="default" @click="bathqueding(item)">批量完成</u-button></span>
						</view>
					</u-col>
				</view>
			</view>
		</u-card>
		<u-popup v-model="chayishow" mode="center" closeable width="600rpx" height="520rpx" border-radius="10">
			<view class="u-text-center u-m-t-20">差异</view>
			<view class=" u-m-20">
				<u-input v-model="iteminfo.num" type="digit" border="true" placeholder="请输入盘点数量"/>
			</view>
			<view class=" u-m-20">
				<!-- <u-input v-model="iteminfo.goodsbatchs" type="select" @click="getT()" border="true" placeholder="请选择生产日期"/> -->
				<u-input v-model="iteminfo.goodsbatchs" border="true" placeholder="请输入批次"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="nodes" type="textarea" border="true" placeholder="请输入描述"/>
			</view>
			<view style="height: 80rpx;text-align: center;">
				<view class="close-btn u-m-t-40">
					<u-button size="medium" @click="queding()">确定</u-button>
				</view>
			</view>
		</u-popup>
		<view>
			<u-calendar v-model="show" mode="date" max-date="2070-01-01" @change="change"></u-calendar>
		</view>
		
		<u-card padding="16" v-if="current == 1 && dataList2">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="10" text-align="left">
							<view class="weikaishi2 lanse">
								<span v-html="'库位：'+dataList2.positionNumber"></span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'库存数：'+dataList2.num"></span>
							</view>
						</u-col>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'库存体积：'+dataList2.volume"></span>
							</view>
						</u-col>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'盘点数：'+dataList2.trueNum"></span>
							</view>
						</u-col>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'盘点体积：'+dataList2.trueVolume"></span>
							</view>
						</u-col>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'托数：'+dataList2.trayNum"></span>
							</view>
						</u-col>
						<u-col span="6">
							<view class="weikaishi4 u-m-t-4">
								<span v-html="'品类：'+dataList2.goodsNum"></span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
		</u-card>
		
		<u-card padding="16" v-for="(item,index) in dataList2.list" :key="index" v-if="current == 1 && dataList2">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="12" text-align="left">
							<view class="weikaishi4">
								<span>名称：{{item.goodsName}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="7">
							<view class="weikaishi6 u-line-1">
								<span>编号：{{item.goodsNumber}}</span>
							</view>
						</u-col>
						<u-col span="5">
							<view class="weikaishi6 u-line-1">
								<span>货号：{{item.goodsNo}}</span>
							</view>
						</u-col>
						<u-col span="7">
							<view class="weikaishi6 u-line-1">
								<span>库存：{{item.num}}*{{item.trayNum}}托</span>
							</view>
						</u-col>
						<u-col span="5">
							<view class="weikaishi6 u-line-1">
								<span>盘点：{{item.trueNum}}件</span>
							</view>
						</u-col>
						<u-col span="7">
							<view class="weikaishi6 u-line-1">
								<span>单位：{{item.goodsUnit}}</span>
							</view>
						</u-col>
						<u-col span="5">
							<view class="weikaishi6 u-line-1">
								<span>包装：{{item.goodsPacking}}</span>
							</view>
						</u-col>
						<u-col span="7">
							<view class="weikaishi6 u-line-1">
								<span>规格：{{item.goodsSpecs}}</span>
							</view>
						</u-col>
						<u-col span="12" text-align="left" v-if="item.xianggui">
							<view class="weikaishi6">
								<span>
								换算：{{parseInt(item.num/item.xianggui)}}箱{{item.num-parseInt(item.num/item.xianggui)*item.xianggui}}盒
								</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="wrap">
					<u-collapse>
						<u-collapse-item title="详情">
							<u-row v-for="(item1,index1) in item.list2" :key="index1">
								<u-col span="12">
									<view class="u-line-1 u-m-t-10 weikaishi6">
										<span>状态：{{item1.inventory}}</span>
									</view>
								</u-col>
								<u-col span="5">
									<view class="u-line-1 u-m-t-4 weikaishi6">
										<span>托盘：{{item1.trayNumber}}</span>
									</view>
								</u-col>
								<u-col span="7">
									<view class="u-line-1 u-m-t-4 weikaishi6">
										<span>类型：{{item1.panType}}</span>
									</view>
								</u-col>
								<u-col span="5">
									<view class="u-line-1 u-m-t-4 weikaishi6">
										<span>库存数：{{item1.num}}</span>
									</view>
								</u-col>
								<u-col span="7">
									<view class="u-line-1 u-m-t-4 weikaishi6">
										<span>盘点数：{{item1.trueNum}}</span>
									</view>
								</u-col>
								<u-col span="12" text-align="left" v-if="item1.xianggui">
									<view class="weikaishi6">
										<span>
										换算：{{parseInt(item1.num/item1.xianggui)}}箱{{item1.num-parseInt(item1.num/item1.xianggui)*item1.xianggui}}盒
										</span>
									</view>
								</u-col>
								<u-col span="6">
									<view class="u-line-1 u-m-t-4 weikaishi6">
										<span>批次：{{item1.goodsbatchs}}</span>
									</view>
								</u-col>
								<u-col span="6">
									<view class="u-line-1 u-m-t-4 weikaishi6">
										<span>盘点：{{item1.newGoodsbatchs}}</span>
									</view>
								</u-col>
								<u-col span="12">
									<view class="u-m-t-4 weikaishi6">
										<span>描述：{{item1.nodes}}</span>
									</view>
								</u-col>
								<u-col span="6" text-align="right" v-if="item1.inventory != '已处理'">
									<view class="u-m-t-4">
										<span><u-button type="error" plain="true" size="default" @click="deletetask(item1.id)">删除</u-button></span>
									</view>
								</u-col>
								<u-col span="6" text-align="right" v-if="item1.inventory != '已处理'">
									<view class="u-m-t-4">
										<span><u-button type="primary" plain="true" size="default" @click="edittasj(item1)">编辑</u-button></span>
									</view>
								</u-col>
							</u-row>
						</u-collapse-item>
					</u-collapse>
				</view>
			</view>
		</u-card>
		<view>
			<u-toast ref="uToast" />
		</view>
		<u-popup v-model="show2" mode="center" closeable width="600rpx" height="600rpx" border-radius="10">
			<view class="u-text-center u-m-t-20">增加库存</view>
			<view class=" u-m-20">
				<u-input v-model="form.goodsName" type="select" @click="getgoo()" placeholder="请选择商品"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="form.trueNum" type="digit" border="true"  placeholder="请输入数量"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="form.newGoodsbatchs" type="select" @click="getbatchs()" border="true" placeholder="请选择生产日期"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="form.goodsbatchs" border="true" placeholder="请输入批次"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="form.nodes" type="textarea" border="true" placeholder="请输入描述"/>
			</view>
			<view style="height: 80rpx;text-align: center;">
				<view class="close-btn u-m-t-40">
					<u-button size="medium" @click="adds()">确定</u-button>
				</view>
			</view>
		</u-popup>
		
		<view>
			<u-calendar v-model="show3" mode="date" max-date="2070-01-01" @change="change2"></u-calendar>
		</view>
		
		<u-popup v-model="show4" mode="center" closeable width="550rpx" height="600rpx" border-radius="10">
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20" style="margin-top: 40rpx;">
				<u-search placeholder="输入商品编号/名称/货号搜索" :clearabled="true" v-model="keyword" border-color="#606266" @custom="search" @search="search"></u-search>
			</view>
			<view class="wrap">
				<u-row v-for="item in glist" :key="item.id" @click="xuanzegoods2(item)">
					<u-col span="12" text-align="center">
						<view class="u-m-t-10 weikaishi lanse">
							<span>{{item.goodsNumber}},{{item.goodsName}},{{item.goodsNo}}</span>
						</view>
					</u-col>
				</u-row>
			</view>
		</u-popup>
		
		<u-popup v-model="show5" mode="center" closeable width="600rpx" height="550rpx" border-radius="10">
			<view class="u-text-center u-m-t-20">编辑</view>
			<view class=" u-m-20">
				<u-input v-model="edit.trueNum" type="digit" border="true"  placeholder="请输入数量"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="edit.newGoodsbatchs" type="select" @click="geteditbatchs()" border="true" placeholder="请选择生产日期"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="edit.goodsbatchs" border="true" placeholder="请输入批次"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="edit.nodes" type="textarea" border="true" placeholder="请输入描述"/>
			</view>
			<view style="height: 80rpx;text-align: center;">
				<view class="close-btn u-m-t-40">
					<u-button size="medium" @click="editqr()">确定</u-button>
				</view>
			</view>
		</u-popup>
		
		<view>
			<u-calendar v-model="show6" mode="date" max-date="2070-01-01" @change="change3"></u-calendar>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [
					{
						name: '待盘详情'
					}, 
					{
						name: '已盘详情'
					},
				],
				current: 0,
				dataList:{},
				dataList2:{},
				glist:[],
				diskId:'',
				positionId:'',
				chayishow:false,
				
				show:false,
				show2:false,
				show3:false,
				show4:false,
				show5:false,
				show6:false,
				keyword:'',
				iteminfo:{},
				
				form:{
					diskId:'',
					goodsId:'',
					goodsNumber:'',
					goodsName:'',
					warehouseId:'',
					warehouseName:'',
					areaId:'',
					areaName:'',
					positionId:'',
					positionNumber:'',
					trueNum:'',
					goodsbatchs:'',
					newGoodsbatchs:'',
					nodes:''
				},
				edit:{
					id:'',
					trueNum:'',
					goodsbatchs:'',
					newGoodsbatchs:'',
					nodes:''
				},
				nodes:''
			};
		},
		onLoad(open) {
			this.diskId = open.diskId
			this.positionId = open.positionId
		},
		onShow() {
			setTimeout(() => {
				this.refresh()
			}, 200)
		},
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		methods: {
			queding(){
				this.$http.post('/rest/PanDian/add',{
				},{
					params: {
						diskId:this.diskId,
						dId:this.iteminfo.id,
						trueNum:this.iteminfo.num,
						newGoodsbatchs:this.iteminfo.goodsbatchs,
						nodes:this.nodes
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					if(res.data.code == 200) {
						this.$refs.uToast.show({
							title: '操作成功',
							type: 'success'
						})
						this.nodes = ''
						this.chayishow = false
						this.refresh()
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							type: 'error'
						})
					}
				})
			},
			change(e){
				this.iteminfo.goodsbatchs = e.result
			},
			getT(item){
				this.show = true
			},
			chayi(item){
				this.iteminfo = item
				this.chayishow = true
			},
			editqr(){
				if(typeof this.edit.trueNum == 'undefined' || this.edit.trueNum == null || this.edit.trueNum == ""){
					uni.showToast({
						title: "请输入数量",
						icon: 'none'
					})
					return
				}
				if(typeof this.edit.newGoodsbatchs == 'undefined' || this.edit.newGoodsbatchs == null || this.edit.newGoodsbatchs == ""){
					uni.showToast({
						title: "请选择批次",
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '正在提交'
				})
				this.$http.post('/rest/PanDian/editStorage',{
				},{
					params: this.edit,
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if(res.data.code == 200) {
						this.show5 = false
						this.$refs.uToast.show({
							title: '添加成功',
							type: 'success'
						})
						this.refresh()
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							type: 'error'
						})
					}
				})
			},
			change3(e){
				this.edit.newGoodsbatchs = e.result
			},
			geteditbatchs(){
				this.show6 = true
			},
			edittasj(item){
				this.edit.id = item.id
				this.edit.trueNum = item.trueNum
				this.edit.goodsbatchs = item.goodsbatchs
				this.edit.newGoodsbatchs = item.newGoodsbatchs
				this.edit.nodes = item.nodes
				this.show5 = true
			},
			deletetask(id){
				uni.showModal({
					content: '确定删除吗？',
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/PanDian/deletetask',{
							},{
								params: {
									id:id,
								},
								header: {
									'Authorization' :uni.getStorageSync('token'),
								}
							}).then(res => {
								if(res.data.code == 200) {
									this.$refs.uToast.show({
										title: '操作成功',
										type: 'success'
									})
									this.refresh()
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			adds(){
				if(typeof this.form.goodsId == 'undefined' || this.form.goodsId == null || this.form.goodsId == ""){
					uni.showToast({
						title: "请选择商品",
						icon: 'none'
					})
					return
				}
				if(typeof this.form.trueNum == 'undefined' || this.form.trueNum == null || this.form.trueNum == ""){
					uni.showToast({
						title: "请输入数量",
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '正在提交'
				})
				this.$http.post('/rest/PanDian/addStorage',{
				},{
					params: this.form,
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if(res.data.code == 200) {
						this.show2 = false
						this.$refs.uToast.show({
							title: '添加成功',
							type: 'success'
						})
						this.form.goodsId = ''
						this.form.goodsNumber = ''
						this.form.goodsName = ''
						this.form.trueNum = ''
						this.form.goodsbatchs = ''
						this.form.newGoodsbatchs = ''
						this.form.nodes = ''
						this.refresh()
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							type: 'error'
						})
					}
				})
			},
			change2(e){
				this.form.newGoodsbatchs = e.result
			},
			getbatchs(){
				this.show3 = true
			},
			xuanzegoods2(item){
				this.form.goodsId = item.id
				this.form.goodsNumber = item.goodsNumber
				this.form.goodsName = item.goodsName
				this.show4 = false
			},
			search(){
				this.$http.post('/rest/storage/goodslist',{
				},{
					params: {
						condition:this.keyword
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					let arr = []
					let inta = 0
					for(let item1 of res.data.data) {
						if(inta != 10){
							arr.push(item1)
						} else {
							break
						}
						inta++
					}
					this.glist = arr
				})
			},
			getgoo(){
				this.show4 = true
				this.search()
			},
			add(item){
				this.form.diskId = this.diskId
				this.form.warehouseId = this.dataList.warehouseId
				this.form.warehouseName = this.dataList.warehouseName
				this.form.areaId = this.dataList.areaId
				this.form.areaName = this.dataList.areaName
				this.form.positionId = this.dataList.positionId
				this.form.positionNumber = this.dataList.positionNumber
				this.show2 = true
			},
			bathqueding(item){
				let dids = []
				let newbats = []
				let truenums = []
				for(let item1 of this.dataList.list) {
					if(item1.goodsId == item.goodsId){
						for(let item2 of item1.list2) {
							dids.push(item2.id)
							newbats.push(item2.goodsbatchs)
							truenums.push(item2.num)
						}
					}
				}
				uni.showModal({
					content: '确定批量提交吗？',
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/PanDian/batchadd?dids='+dids+'&newbats='+newbats+'&truenums='+truenums,{
							},{
								params: {
									diskId:this.diskId,
									areaId:this.dataList.areaId
								},
								header: {
									'Authorization' :uni.getStorageSync('token'),
								}
							}).then(res => {
								if(res.data.code == 200) {
									this.$refs.uToast.show({
										title: '操作成功',
										type: 'success'
									})
									this.refresh()
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			refresh(){
				uni.showLoading({
					title: '加载中'
				})
				if(this.current == 0){
					this.$http.post('/rest/PanDian/positionlist',{
					},{
						params: {
							diskId: this.diskId,
							positionId: this.positionId
						},
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						this.dataList = res.data.data
					})
				} else if (this.current == 1){
					this.$http.post('/rest/PanDian/yipositionlist',{
					},{
						params: {
							diskId: this.diskId,
							positionId: this.positionId
						},
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						this.dataList2 = res.data.data
					})
				}
				uni.hideLoading()
			},
			sectionChange(e){
				this.current = e
				this.refresh()
			},
		}
	};
</script>
<style lang="scss">
	
	.u-body-item {
		padding: 20rpx 15rpx;
	}
	.wrap {
		padding: 10rpx;
	}

	.u-row {
		margin: 10rpx 2rpx;
	}
</style>